using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

[Serializable]
public class RoomInfo{
    public Room room;
    public int num = 1;//最大同样式房间数量
    public int weight;
    public override string ToString(){
        return "[" + room + ", num: " + num + ", wright: " + weight + "]";
    }
}

[CreateAssetMenu(menuName = "Level")]
public class LevelInfo : ScriptableObject
{
    public int levelId;
    public string levelName;
    [Serializable]
    public class RoomGenerationInfo{
        public RoomType type;//房间类型
        public int num = 1;//房间数量
        public List<RoomInfo> roomInfos;//房间信息
        /// <summary>
        /// 从当前房间池里选出一个房间，选出的房间不会再次被选中。
        /// </summary>
        /// <returns></returns>
        public Room GetRandomRoom(){
            MapController controller = MapController.instance;
            int weights = 0;
            foreach(RoomInfo room in roomInfos){
                weights += room.weight;
            }
            int currentWeight = 0;
            int rweight = controller.SeedRandom(1, weights+1);
            foreach(RoomInfo room in roomInfos){
                currentWeight += room.weight;
                if(rweight <= currentWeight){
                    if(--room.num <= 0){
                        roomInfos.Remove(room);//当前同类房间全部选完，从房间池里移除
                    }
                    return room.room;
                }
            }
            Debug.Log("获取房间信息失败:\n" + this + "\n"
                + "currentW:" + currentWeight + "\n"
                + " rW: " + rweight);
            return null;
        }
        public override string ToString(){
            string list = "";
            foreach(RoomInfo item in roomInfos){
                list += item + "\n";
            }
            return "["+type+",\n"+num+",\n"+list+"]";
        }
    }
    public List<RoomGenerationInfo> roomGenerationInfos;
    Dictionary<RoomType, RoomGenerationInfo> roomGenerationInfoDic = new Dictionary<RoomType, RoomGenerationInfo>();
    public RoomGenerationInfo FindRoomInfoByType(RoomType type){
        if(roomGenerationInfoDic.ContainsKey(type)){
            return roomGenerationInfoDic[type];
        }else{
            foreach(var item in roomGenerationInfos){
                if(item.type == type){
                    roomGenerationInfoDic.Add(type, item);
                    return item;
                }
            }
            Debug.Log("LevelInfo: "+type+"类型的房间信息不存在");
            return null;
        }
    }
    /// <summary>
    /// 获取指定类型的随机一个房间
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public Room GetRoom(RoomType type){
        return FindRoomInfoByType(type).GetRandomRoom();
    }
    
}
